# This file is included from */sub/*/Makefile
#

include $(topdir)makefile.inc

CFLAGS+=-DLOW_LEVEL

all: libplatformsub.a

TARGET_PRIMARY=$(PRIMARY_ROOT)/$(PLATFORM)/sub/$(PLATFORMSUB)/PRIMARY.BIN

#OBJS= should be defined in */sub/*/Makefile

libplatformsub.a: $(OBJS)

clean:
	rm -f $(OBJS) libplatformsub.a stubs_entry.S.err stubs_not_found_names.err

distclean: clean
	rm -f stubs_entry.S stubs_auto.S
	rm -rf .dep


stubs_auto.S: $(STUBS_AUTO_DEPS)
	@echo $^ \-\> $@
ifeq ($(OSTYPE),Windows)
	cat $^ | \
	    grep -E 'B[[:alpha:]]*[[:space:]]+sub_[0-9A-Fa-f]' | \
	    sed 's/.*B[[:alpha:]]*[[:space:]][[:space:]]*sub_\\([0-9A-Fa-f]*\\).*/\\1/' | \
	    $(SORT) | uniq | \
	    awk 'BEGIN {print "/* THIS FILE IS GENERATED */\n/* DO NOT EDIT THIS FILE! */\n\n#include \"stubs_asm.h\"\n"} \
	        {printf("STUB(%s)\n",$$1)}' > stubs_auto.S
else	
	cat $^ | \
	    grep -E 'B[[:alpha:]]*[[:space:]]+sub_[0-9A-Fa-f]' | \
	    sed 's/.*B[[:alpha:]]*[[:space:]][[:space:]]*sub_\([0-9A-Fa-f]*\).*/\1/' | \
	    $(SORT) | uniq | \
	    awk 'BEGIN {print "/* THIS FILE IS GENERATED */\n/* DO NOT EDIT THIS FILE! */\n\n#include \"stubs_asm.h\"\n"} \
	        {printf("STUB(%s)\n",$$1)}' > stubs_auto.S
endif

stubs_entry.S: $(TARGET_PRIMARY) $(topdir)tools/signatures_$(PLATFORMOS).h $(topdir)tools/finsig$(EXE)
	@echo \-\> $@
ifdef OPT_GEN_STUBS
	rm -f stubs_entry.S.err
	if [ -s $(TARGET_PRIMARY) ] ; then \
	  $(topdir)tools/finsig $(TARGET_PRIMARY) $(ROMBASEADDR) > stubs_entry.S ; \
	else \
	  echo CAUTION! \'stubs_entry.S\' is not updated due to target firmware binary not found! ; \
	  touch stubs_entry.S ; \
	fi ; \
	if [ "X`grep 'ERROR' stubs_entry.S`" != "X" ] ; then \
	  grep "ERROR" stubs_entry.S | $(ESED) "s/\/\/ ERROR: ([^ ]+) is not found.*/\1/" | $(SORT) | uniq > stubs_nf.names ; \
	  grep -E "(NH?STUB|WRAPPER)" stubs_entry_2.S | $(ESED) "s/ *(NH?STUB|WRAPPER)\( *([^ ,\)]+).*/\2/" | $(SORT) | uniq > stubs_mn.names ; \
	  comm -23 stubs_nf.names stubs_mn.names > stubs_not_found_names.err ; \
	  rm -f stubs_nf.names stubs_mn.names ; \
	  if [ -s stubs_not_found_names.err ] ; then \
	    echo ERROR! There are unresolved addresses in \'stubs_entry.S\' \($(PLATFORM)-$(PLATFORMSUB)\) for the following names: ; \
	    cat stubs_not_found_names.err ; \
	    echo Please, add them into \'stubs_entry_2.S\' manually! ; \
	    mv stubs_entry.S stubs_entry.S.err ; \
	    exit 1 ; \
	  fi ; \
	  rm -f stubs_not_found_names.err ; \
	fi
else
	echo CAUTION! \'stubs_entry.S\' updating is turned off! ; \
	touch stubs_entry.S
endif

$(TARGET_PRIMARY):
	touch $(TARGET_PRIMARY)

include $(topdir)bottom.inc
